from django.shortcuts import render
from django.views.decorators.http import require_http_methods
from .models import APK
import json
from django.http.response import JsonResponse
from .utils.utils import *

# Create your views here.

def show(request):
    return render(request,'blacklist/show.html')

def black_list(request):
    apks = APK.objects.all()
    return render(request,'blacklist/black_list.html',{'apks':apks})

def white_list(request):
    apks = APK.objects.all()
    return render(request,'blacklist/white_list.html',{'apks':apks})

@require_http_methods(['GET','POST'])
def input_apkinfo(request):
    if request.method == "GET":
        return render(request,'blacklist/input_apkinfo.html')
    else:
        data = request.body.decode('utf-8')
        data = str2json(data)
        json_data = json.loads(data)
        new_apk = APK()
        for key,value in json_data.items():
            if hasattr(new_apk,key):
                setattr(new_apk,key,value)
        new_apk.save()
        return JsonResponse({'status': 'success', 'message': '上传成功','data':{'apk_id':new_apk.id}})